/*
* ============================================================================
*  Name     : GV60AppUi.cpp
*  Part of  : GV60
*  Copyright: Wipo Software
* ============================================================================
*/

// INCLUDE FILES
#include <aknnotewrappers.h>
#include <avkon.hrh>
#include <caknfileselectiondialog.h>
#include <mgfetch.h>
#include <pathinfo.h>
#include <apgcli.h>

#include <GV60.rsg>
#include "GV60.pan"
#include "GV60.hrh"
#include "GV60AppUi.h"

#include "GV60PhonebookView.h"
#include "GV60SettingsView.h"

#include "Logger.h"
#include "Version.h"

// ============================ MEMBER FUNCTIONS ===============================
void CGV60AppUi::ConstructL()
{
  // Initialise app UI with standard value.
  BaseConstructL(EAknEnableSkin);

  DEBUGINIT();

  // Initialize members
  m_pConnectingDialog   = NULL;
  m_pCheckVersionDialog = NULL;
  m_pVersionChecker     = NULL;

  // Create view objects
  m_pPhonebookView = new (ELeave) CGV60PhonebookView;
  CleanupStack::PushL(m_pPhonebookView);
  m_pPhonebookView->ConstructL();
  AddViewL(m_pPhonebookView);       // transfer ownership to CAknViewAppUi
  CleanupStack::Pop();              // m_pPhonebookView

  m_pSettingsView = new (ELeave) CGV60SettingsView;
  CleanupStack::PushL(m_pSettingsView);
  m_pSettingsView->ConstructL();
  AddViewL(m_pSettingsView);        // transfer ownership to CAknViewAppUi
  CleanupStack::Pop();              // m_pSettingsView

  // Read configuration
  TBool bConfigRes = m_iConfiguration.ReadFromFile();
  
  // Set default view
  SetDefaultViewL(*m_pPhonebookView);
}

CGV60AppUi::~CGV60AppUi()
{
  if (m_pVersionChecker)
  {
    delete m_pVersionChecker;
    m_pVersionChecker = NULL;
  }
  
  m_pPhonebookView = NULL;
  m_pSettingsView  = NULL;
}

void CGV60AppUi::HandleStatusPaneSizeChange()
{
  CAknAppUi::HandleStatusPaneSizeChange();
}

void CGV60AppUi::HandleResourceChangeL(TInt aType)
{
  CAknAppUi::HandleResourceChangeL(aType);
  
  if (aType == KEikDynamicLayoutVariantSwitch)
  {
    // Call sizechanged on all views
    if (m_pPhonebookView)
    {
      m_pPhonebookView->SizeChanged();
    }
    if (m_pSettingsView)
    {
      m_pSettingsView->SizeChanged();
    }
  }
}

void CGV60AppUi::HandleCommandL(TInt aCommand)
{
  switch( aCommand )
  {
    case EEikCmdExit:
    case EAknSoftkeyExit:
      Exit();
      break;

    case EGV60CmdSettings:
      ActivateLocalViewL(KSettingsViewId);
      break;

    case EGV60CmdCheckNewVersion:
      CheckNewVersionL();
      break;
    
    case EGV60CmdAbout:
      ShowVersionNumberL();
      break;

    default:
      Panic(EGV60Ui);
      break;
  }
}

void CGV60AppUi::DynInitMenuPaneL(TInt aResourceId, CEikMenuPane* /*aMenuPane*/)
{
  if (aResourceId == R_GV60_MENU)
  {
    // Nothing to do right now
  }
}

void CGV60AppUi::DialogDismissedL(TInt aButtonId)
{
  if (EAknSoftkeyCancel == aButtonId)
  {
    m_pCheckVersionDialog = NULL;
    m_pConnectingDialog   = NULL;
    
    if (m_pVersionChecker)
    {
      m_pVersionChecker->CancelRequest();
    }
  }
}

void CGV60AppUi::VersionReady(TInt iResult, TGV60Version iVersion)
{
  // Dismiss dialog
  if (m_pCheckVersionDialog)
  {
    TRAP_IGNORE( m_pCheckVersionDialog->ProcessFinishedL() );
    m_pCheckVersionDialog = NULL;
  }
  
  // Version checker can remain in memory for now...
  // it will be deleted later
  
  // Show error note in case of error
  if (iResult != KErrNone)
  {
    TRAP_IGNORE( ErrorNoteL(R_STRING_GENERIC_ERROR) );
    return;
  }
  
  DEBUG2("Current version: ", KGV60Version.iMajor);
  DEBUG2("Current version: ", KGV60Version.iMinor);
  DEBUG2("Current version: ", KGV60Version.iBuild);
  DEBUG2("Online version: ", iVersion.iMajor);
  DEBUG2("Online version: ", iVersion.iMinor);
  DEBUG2("Online version: ", iVersion.iBuild);
  
  if (
      (iVersion.iMajor > KGV60Version.iMajor) ||
      (iVersion.iMajor == KGV60Version.iMajor && iVersion.iMinor > KGV60Version.iMinor) ||
      (iVersion.iMajor == KGV60Version.iMajor && iVersion.iMinor == KGV60Version.iMinor && iVersion.iBuild > KGV60Version.iBuild)
     )
  {
    TRAP_IGNORE( InformationNoteL(R_STRING_NEW_VERSION_AVAIL) );
    return;
  }
  else if (iVersion.iMajor == KGV60Version.iMajor &&
      iVersion.iMinor == KGV60Version.iMinor &&
      iVersion.iBuild == KGV60Version.iBuild)
  {
    TRAP_IGNORE( InformationNoteL(R_STRING_NEW_VERSION_NOT_AVAIL) );
    return;
  }
  else
  {
    TRAP_IGNORE( InformationNoteL(R_STRING_USING_FUTURE_VERSION) );
  }
}

CGV60Configuration& CGV60AppUi::GetConfig()
{
  return m_iConfiguration;
}

void CGV60AppUi::CheckNewVersionL()
{
  // delete old version checker
  if (m_pVersionChecker)
  {
    delete m_pVersionChecker;
    m_pVersionChecker = NULL;
  }
  
  // create new version checker
  m_pVersionChecker = new VersionChecker(this);
  if (m_pVersionChecker == NULL)
  {
    ErrorNoteL(R_STRING_GENERIC_ERROR);
    return;
  }
  
  if (!m_pCheckVersionDialog)
  {
    m_pCheckVersionDialog = new(ELeave)CAknWaitDialog( (REINTERPRET_CAST(CEikDialog**, &m_pCheckVersionDialog)), ETrue );
    m_pCheckVersionDialog->SetTone( CAknNoteDialog::ENoTone ); 
    m_pCheckVersionDialog->SetCallback(this);
    m_pCheckVersionDialog->ExecuteLD(R_GV60_CHECKING_VERSION_NOTE);
  }

  // Start authentication
  TInt iDownloadRes = m_pVersionChecker->DownloadVersion();
  
  if (iDownloadRes != KErrNone)
  {
    m_pCheckVersionDialog->ProcessFinishedL();
    m_pCheckVersionDialog = NULL;
    
    ErrorNoteL(R_STRING_GENERIC_ERROR);
  }
}

void CGV60AppUi::ShowVersionNumberL()
{
  // Create string with version
  TBuf<10> iVersionString;
  TInt iVer = VersionChecker::VersionToString(KGV60Version, iVersionString);
  if (iVer != KErrNone)
  {
    User::Leave(iVer);
  }
  
  // Load resource string
  HBufC* pText = CCoeEnv::Static()->AllocReadResourceLC(R_STRING_VERSION);
  HBufC* pTextVersion = HBufC::NewLC(pText->Length() + 10);
  
  // Create version string
  pTextVersion->Des().Copy(*pText);
  pTextVersion->Des().Append(iVersionString);

  // Info note
  InformationNoteL(*pTextVersion);
  
  CleanupStack::PopAndDestroy(pTextVersion);
  CleanupStack::PopAndDestroy(pText);
}

void CGV60AppUi::ErrorNoteL(TInt aResourceId, TInt aCause, ENoteType iType)
{
  HBufC* pErrorString = CCoeEnv::Static()->AllocReadResourceLC(aResourceId);
  HBufC* pErrorCompleteString = HBufC::NewLC(pErrorString->Length() + 10);

  // Add cause
  pErrorCompleteString->Des().Copy(*pErrorString);
  pErrorCompleteString->Des().Append(_L("("));
  pErrorCompleteString->Des().AppendNum(aCause);
  pErrorCompleteString->Des().Append(_L(")"));
  
  ErrorNoteL(*pErrorCompleteString, iType);

  CleanupStack::PopAndDestroy(pErrorCompleteString);
  CleanupStack::PopAndDestroy(pErrorString);
}

void CGV60AppUi::ErrorNoteL(TInt aResourceId, ENoteType iType)
{
  HBufC* pErrorString = CCoeEnv::Static()->AllocReadResourceLC(aResourceId);
  ErrorNoteL(*pErrorString, iType);
  CleanupStack::PopAndDestroy(pErrorString);
}

void CGV60AppUi::ErrorNoteL(const TDesC& iText, ENoteType iType)
{
  TBool bWaitingDialog = EFalse;
  if (iType == eNoteBlockingStandard || iType == eNoteBlockingNoTimeout)
  {
    bWaitingDialog = ETrue;
  }
  
  CAknErrorNote* pErrorNote = new (ELeave) CAknErrorNote(bWaitingDialog);
  if (iType == eNoteNoTimeout || iType == eNoteBlockingNoTimeout)
  {
    pErrorNote->SetTimeout(CAknNoteDialog::ENoTimeout);
  }
  pErrorNote->ExecuteLD(iText);
}

void CGV60AppUi::InformationNoteL(TInt aResourceId, ENoteType iType)
{
  HBufC* pInfoString = CCoeEnv::Static()->AllocReadResourceLC(aResourceId);
  InformationNoteL(*pInfoString, iType);
  CleanupStack::PopAndDestroy(pInfoString);
}

void CGV60AppUi::InformationNoteL(const TDesC& iText, ENoteType iType)
{
  TBool bWaitingDialog = EFalse;
  if (iType == eNoteBlockingStandard || iType == eNoteBlockingNoTimeout)
  {
    bWaitingDialog = ETrue;
  }

  CAknInformationNote* pAboutNote = new (ELeave) CAknInformationNote(bWaitingDialog);
  if (iType == eNoteNoTimeout || iType == eNoteBlockingNoTimeout)
  {
    pAboutNote->SetTimeout(CAknNoteDialog::ENoTimeout);
  }
  pAboutNote->ExecuteLD(iText);
}

void CGV60AppUi::ConfirmationNoteL(TInt aResourceId)
{
  HBufC* pConfirmationString = CCoeEnv::Static()->AllocReadResourceLC(aResourceId);
  ConfirmationNoteL(*pConfirmationString);
  CleanupStack::PopAndDestroy(pConfirmationString);
}

void CGV60AppUi::ConfirmationNoteL(const TDesC& iText)
{
  CAknConfirmationNote* pConfirmationNote = new (ELeave) CAknConfirmationNote(ETrue);
  pConfirmationNote->SetTimeout(CAknNoteDialog::ENoTimeout);
  pConfirmationNote->ExecuteLD(iText);
}

// End of file
